package com.zzy.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.common.State;
import com.zzy.common.Successful;
import com.zzy.common.ZZYUtils;
import com.zzy.entity.*;
import com.zzy.mapper.EssayMapper;
import com.zzy.mapper.UserLikeMapper;
import com.zzy.service.EssayService;
import com.zzy.service.UserLikeService;
import com.zzy.service.impl.EssayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class UserLikeController {

    @Autowired
    private UserLikeService userLikeService;
    @Autowired
    private EssayService essayService;
    @Autowired
    private EssayMapper essayMapper;
    @Autowired
    private UserLikeMapper userLikeMapper;

    @PostMapping("/setlike/{eid}")
    public Successful<Boolean> setLike(@PathVariable Integer eid,@CookieValue("uid") Integer UId,String classify,String content){

    Successful<Boolean> successful = new Successful<>();
    System.out.println("用户Id"+UId);
    System.out.println("classift"+classify);
    System.out.println("content"+content);
    //插入数据
    UserLike userLike = new UserLike(UId,eid,new Date(),classify,content);
    userLikeMapper.insert(userLike);
    successful.setState(State.SUCCEED);
    successful.setMessage("添加成功");
    successful.setData(true);

    return successful;
}

    @GetMapping("/getuserlikes/{classify}")
    public Successful<List<Params>> getUserLikes(@PathVariable String classify, @CookieValue("uid") Integer uid){
        System.out.println(uid);
        System.out.println(classify);
        Successful<List<Params>> successful = new Successful<>();
        List<Params> list = new ArrayList<>();
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classify",classify);
        queryWrapper.eq("u_id",uid);
        List<UserLike> userLikes = userLikeMapper.selectList(queryWrapper);
        for (UserLike userLike : userLikes){
            Essay essay = essayMapper.selectById(userLike.getEId());
            Params params = new Params();
            params.setP1(essay.getTitle());
            params.setP2(essay.getAuthor());
            params.setP3(userLike.getContent());
            params.setP4(userLike.getCreateTime());
            params.setP5(essay.getEId());
            list.add(params);
        }
        successful.setState(State.SUCCEED);
        successful.setMessage("查找成功");
        successful.setData(list);

        return successful;
    }

    @DeleteMapping("/delUserLike/{eid}")
    public Successful<String> delUserLike(@PathVariable Integer eid,@CookieValue("uid") Integer uid){
        Successful<String> successful = new Successful<>();
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("e_id",eid);
        queryWrapper.eq("u_id",uid);
        userLikeMapper.delete(queryWrapper);
        successful.setState(State.SUCCEED);
        successful.setMessage("取消收藏成功");
        successful.setData("删除成功");
        return successful;
    }

}
